import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:interview/servers/question_list.dart';
import 'package:interview/utils/http.dart';
import 'package:interview/utils/logger.dart';

class QuestionDetail extends Question {
  final String content; // 添加 content 字段
  final String answer;  // 处理后的答案或内容
  final RxInt collectFlag; // 是否收藏 (0 或 1)
  final RxInt likeFlag; // 是否点赞 (0 或 1)
  final List<String> stage; // 所属模块
  final String createdAt; // 创建时间
  final String creatorName; // 创建人
  final String creatorAvatar; // 创建人头像

  QuestionDetail({
    required String id,
    required String stem,
    required int difficulty,
    required RxInt likeCount,
    required RxInt views,
    required int readFlag,
    required this.content, // 添加 content
    required this.answer,
    required this.collectFlag,
    required this.likeFlag,
    required this.stage,
    required this.createdAt,
    required this.creatorName,
    required this.creatorAvatar,
  }) : 
  super(
    id: id,
    stem: stem,
    difficulty: difficulty,
    likeCount: likeCount,
    views: views,
    readFlag: readFlag,
  );

  // JSON 转换
  factory QuestionDetail.fromJson(Map<String, dynamic> json) {
    try {
      // 提取 content 和 answer 字段
      final String answerValue = json['answer'] as String? ?? '';
      final String contentValue = json['content'] as String? ?? '';

      return QuestionDetail(
        id: json['id']?.toString() ?? '',
        stem: json['stem'] as String? ?? '',
        difficulty: json['difficulty'] as int? ?? 0,
        likeCount: (json['likeCount'] as int? ?? 0).obs,
        views: (json['views'] as int? ?? 0).obs,
        readFlag: json['readFlag'] as int? ?? 0,
        answer: answerValue,
        content: contentValue, // 解析 content
        collectFlag: (json['collectFlag'] as int? ?? 0).obs,
        likeFlag: (json['likeFlag'] as int? ?? 0).obs,
        stage: (json['stage'] as List<dynamic>? ?? []).cast<String>(),
        createdAt: json['createdAt'] as String? ?? '',
        creatorName: json['creatorName'] as String? ?? '',
        creatorAvatar: json['creatorAvatar'] as String? ?? '',
      );
    } catch (e) {
      Logger.error('JSON 解析错误: $e, 原始数据: $json', tag: 'QuestionDetail.fromJson');
      rethrow;
    }
  }

  @override
  Map<String, dynamic> toJson() {
    final map = super.toJson();
    map.addAll({
      'answer': answer,
      'content': content, // 添加 content
      'collectFlag': collectFlag.value,
      'likeFlag': likeFlag.value,
      'stage': stage,
      'createdAt': createdAt,
      'creatorName': creatorName,
      'creatorAvatar': creatorAvatar,
    });
    return map;
  }
}


Future<QuestionDetail> getQuestionDetail(String id , BuildContext context) async {
  try {
    final response = await Http.get<Map<String, dynamic>>('question/$id' , context: context);
    return QuestionDetail.fromJson(response["data"]);
  } catch (e) {
    Logger.error("获取试题详情失败： $e" , tag: "getQuestionDetail" );
    throw Exception("获取试题详情失败： $e");
  }
}
